<?php

namespace App\Admin\Controllers;

use App\Miya\Models\Classify;
use App\Miya\Models\Debris;
use App\Miya\Models\Info;
use App\Miya\Models\Module;
use App\Miya\Tools\TagTool;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Table;
use Illuminate\Validation\Rule;

class DebrisController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '碎片管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Debris());
        $grid->model()->with(['module']);

        $grid->column('id', '序号');
        $grid->column('name', '名称')->label();
        $grid->column('tag', '标签')->label();
        $grid->column('module_id', '模块')->display(function () {
            return $this->module->name;
        })->label();
        $grid->column('classifies', '信息来源分类')->display(function () {
            $count = count($this->classifies);
            if ($count > 0) {
                return count($this->classifies_with_descendants);
            } else {
                return $this->module->name;
            }
        })->modal(function ($model) {
            $count = count($model->classifies);
            if ($count > 0) {
                $data = Classify::whereIn('id', $model->classifies_with_descendants)->select(['id', 'name'])->get()->toArray();
            } else {
                $data = Classify::where('module_id', $model->module_id)->select(['id', 'name'])->get()->toArray();
            }

            return new Table(['序号', '分类'], $data);
        });
        $grid->column('num', '信息数量')->label();
        $grid->column('rank_type', '排序方式')->editable('select', Info::$rankTypes);

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Debris());

        $form->tab('基础设置', function (Form $form) {
            $form->text('name', '碎片名')->rules(['required']);
            $form->select('module_id', '模块')
                ->options(Module::pluck('name', 'id'))
                ->rules(['required'])->load('classifies', '/api/admin/debris/get-classifies');
            $classifies = Classify::selectOptions();
            unset($classifies[0]);
            $form->multipleSelect('classifies', '信息来源分类')->options($classifies);
        })->tab('高级设置', function (Form $form) {
            if ($form->isCreating()) {
                $form->text('tag', '标签')->rules([Rule::unique('debris')]);
            } elseif ($form->isEditing()) {
                $debris = Debris::find(request()->route('debris'));
                $form->text('tag', '标签')->rules([Rule::unique('debris')->ignore($debris)]);
            }
            $form->number('num', '信息数量')->default(10)->rules(['required']);
            $form->select('rank_type', '排序方式')->default(1)->options(Info::$rankTypes)->rules(['required']);
        });

        $form->saving(function (Form $form) {
            if ($form->name && !$form->tag) {
                $form->tag = TagTool::makeTag($form->name, config('miya.tag_suffix.debris'));
            }
        });

        return $form;
    }
}
